package com.bitejiuyeke.bitecommoncache.utils;

import com.bitejiuyeke.bitecommonredis.service.RedisService;
import com.fasterxml.jackson.core.type.TypeReference;

import com.github.benmanes.caffeine.cache.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.util.concurrent.TimeUnit;

public class CacheUtil {
    private static final Logger log = LoggerFactory.getLogger(CacheUtil.class);

    public static <T> T getL2Cache(RedisService redisService, String key, TypeReference<T> typeReference,
                                   Cache<String,Object> cache){
        T res =(T)cache.getIfPresent(key);

        if(res!=null){
            log.info("正在读取本地缓存:{}",res);
            return res;
        }

        res=redisService.getCacheObject(key,typeReference);


        if(res!=null){
            log.info("正在读取resid缓存:{}",res);
            cache.put(key,res);
            return res;
        }

        return null;




    }


    public static <T> void setL2Cache(RedisService redisService, String key,
                                      T value,Cache<String,Object> caffeineCache, final Long timeout, final
                                      TimeUnit timeUnit){
// redis缓存
        redisService.setCacheObject(key,value, timeout, timeUnit);
        log.info("更新redis缓存信息"+ key);
// 本地缓存
        caffeineCache.put(key,value );
        log.info("更新本缓存信息"+ key);
    }



}
